/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.request.ShapeSize;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.shaper.RoundRectImageShaper;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Locale;

/**
 * RoundRectImageShaperTestPageView
 */
public class RoundRectImageShaperTestPageView extends DirectionalLayout {
    private SketchImageView mImageRoundRect;
    private Slider mSliderRadius;
    private Slider mSliderStroke;

    private DisplayOptions mDisplayOptions;

    public RoundRectImageShaperTestPageView(Context context) {
        super(context, null);
        initView();
    }

    private void initView() {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        Component view = layoutScatter.parse(ResourceTable.Layout_round_rect_image_shaper_page_view, this, true);

        mImageRoundRect = (SketchImageView) view.findComponentById(ResourceTable.Id_image_roundRectImageShaper);

        Text textRadius = (Text) view.findComponentById(ResourceTable.Id_text_roundRectImageShaper_radius);
        Text textStroke = (Text) view.findComponentById(ResourceTable.Id_text_roundRectImageShaper_stroke);

        mSliderRadius = (Slider) view.findComponentById(ResourceTable.Id_slider_radius).findComponentById(ResourceTable.Id_slider);
        mSliderRadius.setMaxValue(100);
        mSliderRadius.setValueChangedListener(new ValueChangedListener(textRadius));
        mSliderRadius.setProgressValue(20);

        mSliderStroke = (Slider) view.findComponentById(ResourceTable.Id_slider_stroke).findComponentById(ResourceTable.Id_slider);
        mSliderStroke.setMaxValue(100);
        mSliderStroke.setValueChangedListener(new ValueChangedListener(textStroke));
        mSliderStroke.setProgressValue(5);

        final int componentWidth = Utils.getDisplayWidthInPx(getContext())
                - mImageRoundRect.getMarginLeft() * 2;
        final double aspectRatios = 640 / (double) 789;
        mDisplayOptions = new DisplayOptions().setShapeSize(new ShapeSize(componentWidth, (int) (componentWidth / aspectRatios)));
        mDisplayOptions.setBitmapPoolDisabled(true);
        mDisplayOptions.setCacheInDiskDisabled(true);
        mDisplayOptions.setCacheInMemoryDisabled(true);
        mDisplayOptions.setCacheProcessedImageInDisk(false);

        DirectionalLayout.LayoutConfig layoutConfig = (DirectionalLayout.LayoutConfig) mImageRoundRect.getLayoutConfig();
        if (layoutConfig != null) {
            layoutConfig.height = (int) (componentWidth / aspectRatios);
            mImageRoundRect.setLayoutConfig(layoutConfig);
        }
        apply();
    }

    private void apply() {
        final float scaleRatio = 1.5f;
        RoundRectImageShaper roundRectImageShaper = new RoundRectImageShaper(mSliderRadius.getProgress() * scaleRatio)
                .setContext(getContext())
                .setResID(ResourceTable.Media_sample);
        roundRectImageShaper.setStroke(Color.WHITE.getValue(), (int) (mSliderStroke.getProgress() * scaleRatio));
        mDisplayOptions.setShaper(roundRectImageShaper);
        mImageRoundRect.setOptions(mDisplayOptions);
        Sketch.with(getContext()).displayFromResource(ResourceTable.Media_sample, mImageRoundRect).commit();
    }

    private class ValueChangedListener implements Slider.ValueChangedListener {
        private final Text text;

        public ValueChangedListener(Text text) {
            this.text = text;
        }

        @Override
        public void onProgressUpdated(Slider slider, int i1, boolean b) {
            text.setText(String.format("%d/%d", i1, 100, Locale.ENGLISH));
        }

        @Override
        public void onTouchStart(Slider slider) {

        }

        @Override
        public void onTouchEnd(Slider slider) {
            apply();
        }
    }
}
